package com.tszs.Controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.tszs.bean.Car;
import com.tszs.biz.CommodityListBiz;
import com.tszs.biz.MessageManagementBiz;
import com.tszs.biz.PersonalOrderBiz;
import com.tszs.entity.CommodityList;
import com.tszs.entity.MessageManagement;
import com.tszs.entity.MessageManagementExample;
import com.tszs.entity.PersonalOrder;
import com.tszs.entity.PersonalOrderExample;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;
import com.tszs.util.projectPath;

import net.sf.json.JSONObject;

/**
 * @类名:MessageManagementController
 * @类描述:留言管理
 * @创建时间: 2019-03-21 11:49:11
 * @author 王俊
 *
 */
@Controller
public class MessageManagementController {
	/**
	 * @PersonalOrderBiz
	 */
	@Autowired
	private PersonalOrderBiz personalOrderBiz;
	/**
	 * @MessageManagementController
	 */
	@Autowired
	private MessageManagementBiz messageManagementBiz;
	/**
	 * @CommodityListBiz
	 */
	@Autowired
	private CommodityListBiz commodityListBiz;

	/**
	 * @类名:MessageManagementpage
	 * @类描述:留言管理分页
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 *
	 */
	@RequestMapping("pc/admin/MessageManagementpage")
	public void MessageManagementpage(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String type = request.getParameter("type");
		String tatus = request.getParameter("tatus");
		MessageManagement example = new MessageManagement();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		example.setmTatus(tatus);
		List<MessageManagement> selectByExample = messageManagementBiz.selectByExamplePage(example);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {

			Map<String, Object> mapData = new HashMap<String, Object>();
			String b="";
			if(StringUtils.isNotEmpty(selectByExample.get(i).getmPhoto())) {
				for (int c = 0; c < selectByExample.get(i).getmPhoto().split(",").length; c++) {
					b+="<a target=\"_Blank\" href='"+projectPath.url+selectByExample.get(i).getmPhoto().split(",")[c]+"'><img src='"+projectPath.url+selectByExample.get(i).getmPhoto().split(",")[c]+"'/></a>";
				}
			}
			
			mapData.put("mId", i + 1);
//			mapData.put("mPhoto", b);
			mapData.put("mContent", selectByExample.get(i).getmContent());
			mapData.put("UserId", selectByExample.get(i).getUserId());
			mapData.put("UserName", selectByExample.get(i).getUserName());
			if (type.equals("1")) {
				mapData.put("mStatic",
						selectByExample.get(i).getmType().equals("0") ? "<a style='color:green;'>未确认</a>"
								: "<a style='color:red;'>已确认</a>");
				if("0".equals(selectByExample.get(i).getmType())){
				mapData.put("button", "<a href=\"javascript:table('2','" + selectByExample.get(i).getmId()
						+ "')\" class=\"layui-btn layui-btn-danger layui-btn-xs\" style=''>已确认</a>");
				}
			} else {
				String cd="";
				if(StringUtils.isNotEmpty(selectByExample.get(i).getReplyPhoto())) {
					for (int c = 0; c < selectByExample.get(i).getReplyPhoto().split(",").length; c++) {
						cd+="<a target=\"_Blank\" href='"+projectPath.url+selectByExample.get(i).getReplyPhoto().split(",")[c]+"'><img src='"+projectPath.url+selectByExample.get(i).getReplyPhoto().split(",")[c]+"'/></a>";
					}
				}
				mapData.put("mStatic",
						selectByExample.get(i).getmStatic().equals("2") ? "<a style='color:green;'>展示</a>"
								: "<a style='color:red;'>不展示</a>");
				mapData.put("AdminId", selectByExample.get(i).getAdminId());
				mapData.put("AdminName", selectByExample.get(i).getAdminName());

				mapData.put("ReplyPhoto", cd);
				mapData.put("ReplyContent", selectByExample.get(i).getReplyContent());
				if (selectByExample.get(i).getmType().equals("0")) {
					mapData.put("mType", "<a style='color:red;'>未回复</a>");
					mapData.put("button", "<a href=\"javascript:table2('0','" + selectByExample.get(i).getmId()
							+ "')\" class=\"layui-btn layui-btn-xs\" style=''>立即回复</a>");
				} else {
					mapData.put("mType", "<a style='color:green;'>已回复</a>");
					mapData.put("button", "<a href=\"javascript:table2('1','" + selectByExample.get(i).getmId()
							+ "')\" class=\"layui-btn layui-btn-danger layui-btn-xs\" style=''>删除</a>");
				}
			}
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}

	/**
	 * @类名:MessageManagementUpdate
	 * @类描述:留言是否展示
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 *
	 */
	@RequestMapping("pc/admin/MessageManagementUpdate")
	public void MessageManagementUpdate(HttpServletRequest request, HttpServletResponse response) {
		MessageManagement record = new MessageManagement();
		record.setmType("1");
		MessageManagementExample example = new MessageManagementExample();
		com.tszs.entity.MessageManagementExample.Criteria ca = example.createCriteria();
		ca.andMIdEqualTo(request.getParameter("id"));
		int updateByExampleSelective = messageManagementBiz.updateByExampleSelective(record, example);
		if (updateByExampleSelective > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @类名:findmessage
	 * @类描述:留言是否展示
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 *
	 */
	@RequestMapping("pc/admin/findmessage")
	public void findmessage(HttpServletRequest request, HttpServletResponse response) {
		MessageManagement example = new MessageManagement();
		example.setmId(request.getParameter("id"));
		List<MessageManagement> list = messageManagementBiz.findmessage(example);
		List<MessageManagement> lists = new ArrayList<>();
		for (int j = 0; j < list.size(); j++) {
			MessageManagement m = new MessageManagement();
			String b = "";
			for (int c = 0; c < list.get(j).getmPhoto().split(",").length; c++) {
				b += "<a target=\"_Blank\" href='"+projectPath.url + list.get(j).getmPhoto().split(",")[c]
						+ "'><img src='"+projectPath.url + list.get(j).getmPhoto().split(",")[c]
						+ "'/></a><br>";
			}
			m.setmPhoto(b);
			m.setmContent(list.get(j).getmContent());
			m.setUserId(list.get(j).getUserId());
			m.setUserName(list.get(j).getUserName());
			lists.add(m);
		}
		new CallBack().yes(response, lists, "");
	}

	/**
	 * @类名:findmessageUpdate
	 * @类描述:留言回复
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 *
	 */
	@RequestMapping("pc/admin/findmessageUpdate")
	public void findmessageUpdate(HttpServletRequest request, HttpServletResponse response) {
		MessageManagement record = new MessageManagement();
		record.setAdminId(request.getSession().getAttribute("aId") + "");
		record.setAdminName(request.getSession().getAttribute("aUsername") + "");
		record.setReplyContent(request.getParameter("cNum"));
		record.setReplyPhoto(request.getParameter("cPhoto"));
		record.setmType("1");
		MessageManagementExample example = new MessageManagementExample();
		com.tszs.entity.MessageManagementExample.Criteria ca = example.createCriteria();
		ca.andMIdEqualTo(request.getParameter("id"));
		int updateByExampleSelective = messageManagementBiz.updateByExampleSelective(record, example);
		if (updateByExampleSelective > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}
	/**
	 * @方法名称：UploadServlet
	 * @方法描述：类别管理文件上传 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 * @param file
	 * @param request
	 * @param response
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("txr/UploadServlet")
	@ResponseBody
	public void UploadServlet(@RequestParam("file") MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) throws IllegalStateException, IOException {
/*		String realPath = request.getSession().getServletContext().getRealPath("/images//");
		System.err.println(realPath);
		File filepath = new File(realPath);
		if (!filepath.exists()) // 判断文件夹是否存在
			filepath.mkdirs();// 创建此抽象路径名指定的目录，包括所有必需但不存在的父目录。
		// 文件保存路径
		String fileName = file.getOriginalFilename();
		String extensionname = fileName.substring(fileName.lastIndexOf(".") + 1);
		String fileNames = Uuid.getUUID();
		String newFilename = fileNames + "." + extensionname;
		String savePath = realPath + "\\" + newFilename;
		file.transferTo(new File(savePath));
		System.err.println(savePath);
		Map<String, Object> map2 = new HashMap<>();
		map2.put("src", newFilename);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", map2);*/
		String fileName = file.getOriginalFilename();
		String extensionname = fileName.substring(fileName.lastIndexOf(".") + 1);
		String fileNames = Uuid.getUUID();
		String newFilename = "D:\\txrImages\\" +fileNames + "." + extensionname;
		file.transferTo(new File(newFilename));
		Map<String, Object> map2 = new HashMap<>();
		map2.put("src", fileNames + "." + extensionname);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", map2);
		new CallBack().yes(response, map, "");
	}
	/**
	 * @类名:insertmessageUpdate
	 * @类描述:留言添加
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 * @throws UnsupportedEncodingException 
	 *
	 */
	@RequestMapping("txr/insertmessageUpdate")
	public void insertmessageUpdate(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		String pid=request.getParameter("pId");
		MessageManagement recor=new MessageManagement();
		recor.setpId(pid);
		recor.setmContent(request.getParameter("mContent"));
		recor.setmStatic("0");
		recor.setUserId(request.getSession().getAttribute("openid")+"");
		recor.setUserName(request.getSession().getAttribute("nickname")+"");
		recor.setmId(Uuid.getUUID());
		recor.setmType("0");
		int size = messageManagementBiz.insertSelective(recor);
		PersonalOrder record=new PersonalOrder();
		record.setPj("1");
		PersonalOrderExample example=new PersonalOrderExample();
		com.tszs.entity.PersonalOrderExample.Criteria ca=example.createCriteria();
		ca.andPIdEqualTo(pid);
		personalOrderBiz.updateByExampleSelective(record, example);
		if(size>0) {
			new CallBack().yes(response, "", "");
		}else {
			new CallBack().no(response, "", "");
		}
	}
	/**
	 * @findcPhtoto
	 * @辅助方法查询图片
	 * @param cId
	 * @return
	 */
	public List<CommodityList> findcPhtoto(String cId) {
		CommodityList example=new CommodityList();
		example.setcId(cId);
		List<CommodityList> findcPhtoto=commodityListBiz.findcPhtoto(example);
		return findcPhtoto;
	}
	/***
	 * @方法名：Mdatails
	 * @方法描述：留言详情
	 * @param request
	 * @param response
	 */
	@RequestMapping("txr/Mdatails")
	public void sadsa(HttpServletRequest request,HttpServletResponse response) {
		PersonalOrderExample example=new PersonalOrderExample();
		com.tszs.entity.PersonalOrderExample.Criteria ca=example.createCriteria();
		ca.andUIdEqualTo("oqqrN54lkwFz7nHEcA56Ja0lpK2w");
		ca.andPjEqualTo("1");
		List<PersonalOrder> selectByExample=personalOrderBiz.selectByExample(example);
		List<Car> list=new ArrayList<>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Car car=new Car();
			car.setP_id(selectByExample.get(i).getpId());
			car.setProduct_id(selectByExample.get(i).getProductId());
			car.setU_name(selectByExample.get(i).getuName());
			car.setProduct_num(selectByExample.get(i).getProductNum());
			List<CommodityList> findcPhtoto=findcPhtoto(selectByExample.get(i).getProductId());
			for (int j = 0; j < findcPhtoto.size(); j++) {
				car.setPhtoto(findcPhtoto.get(j).getcPhtoto().split(",")[0]);
				car.setC_name(findcPhtoto.get(j).getcName());
				car.setC_describe(findcPhtoto.get(j).getcDescribe());
			}
			MessageManagementExample exampleMessageManagementExample=new MessageManagementExample();
			com.tszs.entity.MessageManagementExample.Criteria cad=exampleMessageManagementExample.createCriteria();
			cad.andPIdEqualTo(selectByExample.get(i).getpId());
			List<MessageManagement> selectByExamplePage=messageManagementBiz.selectByExample(exampleMessageManagementExample);
			for (int d = 0; d < selectByExamplePage.size(); d++) {
				car.setMcontent(selectByExamplePage.get(d).getmContent());
			}
			list.add(car);
		}
		new CallBack().yes(response, list, "");
	}
	
	
	/**
	 * @类名:offlinePurchase
	 * @类描述:线下支付留言
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 * @throws UnsupportedEncodingException 
	 *
	 */
	@RequestMapping("txr/offlinePurchase")
	public void offlinePurchase(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		String pid=	Uuid.getUUID();
		MessageManagement recor=new MessageManagement();
		recor.setpId(pid);
		recor.setmContent(request.getParameter("cphone"));
		recor.setmStatic("0");
		recor.setmPhoto(request.getParameter("photo"));
		recor.setUserName(request.getParameter("cname"));
		recor.setmId(Uuid.getUUID());
		recor.setmType("0");
		recor.setmTatus("1");
		int size = messageManagementBiz.insertSelective(recor);
		new  CallBack().yes(response, size, "");
	}
	/**
	 * @类名:offlinePurchase
	 * @类描述:线下支付留言
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 * @throws UnsupportedEncodingException 
	 *
	 */
	@RequestMapping("txr/showofflinePurchase")
	public void showofflinePurchase(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		MessageManagementExample example=new MessageManagementExample();
		com.tszs.entity.MessageManagementExample.Criteria ca=example.createCriteria();
		ca.andUserIdEqualTo(request.getSession().getAttribute("openid")+"");
		ca.andMTatusEqualTo("1");
		ca.andMStaticNotEqualTo("1");
		List<MessageManagement> selectByExample=messageManagementBiz.selectByExample(example);
		new CallBack().yes(response, selectByExample, "");
	}
	

	/**
	 * @类名:offlinePurchase
	 * @类描述:在线留言添加
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 * @throws UnsupportedEncodingException 
	 *
	 */
	@RequestMapping("txr/offlinePurchaseone")
	public void offlinePurchaseone(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		String pid=	Uuid.getUUID();
		MessageManagement recor=new MessageManagement();
		recor.setpId(pid);
		recor.setmContent(request.getParameter("mContent"));
		recor.setmStatic("0");
		recor.setUserId(request.getSession().getAttribute("openid")+"");
		recor.setUserName(request.getSession().getAttribute("nickname")+"");
		recor.setmId(Uuid.getUUID());
		recor.setmType("0");
		recor.setmTatus("2");
		recor.setmCreatetime(Uuid.getTime());
		int size = messageManagementBiz.insertSelective(recor);
		new  CallBack().yes(response, size, "");
	}
	/**
	 * @类名:offlinePurchase
	 * @类描述:在线留言集合
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 * @throws UnsupportedEncodingException 
	 *
	 */
	@RequestMapping("txr/showofflinePurchaseone")
	public void showofflinePurchaseone(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		MessageManagementExample example=new MessageManagementExample();
		com.tszs.entity.MessageManagementExample.Criteria ca=example.createCriteria();
		ca.andUserIdEqualTo(request.getSession().getAttribute("openid")+"");
		ca.andMTatusEqualTo("2");
		ca.andMStaticNotEqualTo("1");
		List<MessageManagement> selectByExample=messageManagementBiz.selectByExample(example);
		new CallBack().yes(response, selectByExample, "");
	}
	
	
	
	
	/**
	 * @类名:findatalisofflinePurchase
	 * @类描述:在线留言详情
	 * @创建时间: 2019-03-21 11:49:11
	 * @author 王俊
	 * @throws UnsupportedEncodingException 
	 *
	 */
	@RequestMapping("txr/findatalisofflinePurchase")
	public void findatalis(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		MessageManagementExample example=new MessageManagementExample();
		com.tszs.entity.MessageManagementExample.Criteria ca=example.createCriteria();
		ca.andMIdEqualTo(request.getParameter("mId"));
		List<MessageManagement> selectByExample=messageManagementBiz.selectByExample(example);
		new CallBack().yes(response, selectByExample, "");
	}
}